package com.bear.cmcc.NetTestHTTP.ui.view;

import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.service.LocalUpdateVillageDBService;
import com.bear.cmcc.NetTestHTTP.util.SetUtil;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 导入小区信息至数据库的进度界面
 * @author meizhen.xu
 *
 */
public class UpdateCellDBView extends LinearLayout {
	
	private Context mContext;
	//private View view ;
	private RelativeLayout cellDBLayout;
	private ProgressBar cellPro;//进度条
	private TextView cellMsgText;//进度条信息
	private Button cancelButton;//取消按钮
	private Button backRunButton;//后台运行按钮
	
	private CellDBInsertReceiver receiver = new CellDBInsertReceiver();//广播

	public UpdateCellDBView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();
	}
	
	public UpdateCellDBView(Context context) {
		super(context);
		this.mContext = context;
		init();
	}

	/**
	 *  初始化界面
	 */
	private void init() {
		createView();
		setViewProperty();
		IntentFilter filter = new IntentFilter();
        filter.addAction(MyCommonConstant.PrivateActionName.NET_WORK_VILLAGE_DATA_DOWN);
        mContext.registerReceiver(receiver, filter);
	}
	/**
	 * 实例化控件
	 */
	private void createView() {
		View view = LayoutInflater.from(mContext).inflate(R.layout.update_cell_db, null);
		this.cellDBLayout = (RelativeLayout)view.findViewById(R.id.upCellDB_cellDBLayout);
		this.cellPro = (ProgressBar)view.findViewById(R.id.upCellDB_cellPro);
		this.cellMsgText = (TextView)view.findViewById(R.id.upCellDB_cellMsgText);
		this.cancelButton = (Button)view.findViewById(R.id.upCellDB_cancelButton);
		backRunButton = (Button)view.findViewById(R.id.upCellDB_backRunButton);
		
		this.addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	}
	/**
	 * 设置控件属性
	 */
	private void setViewProperty() {
		this.hide();
		
		cellDBLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
			}
		});
		
		cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//取消更新小区数据库
				SetUtil.stopImportVillageInfo(mContext);
				hide();
			}
		});
		
		backRunButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//后台运行
				NetworkTestApp.isBackRun_celldb = true;
				Intent intent = new Intent(mContext, LocalUpdateVillageDBService.class);
				mContext.startService(intent);
				hide();
			}
		});
	}
	
	public void onDestroy() {
		//注销广播监听
		if(receiver!=null){
			mContext.unregisterReceiver(receiver);
		}
	}
	
	 /**
     * 导入数据库
     * 
     * @author meizhen.xu
     */
    class CellDBInsertReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    MyCommonConstant.PrivateActionName.NET_WORK_VILLAGE_DATA_DOWN)) {
            	
            	if(NetworkTestApp.isStop_celldb){
            		SetUtil.stopImportVillageInfo(context);
            		hide();
            		return ;
            	}
            	
                String type = intent
                        .getStringExtra(MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_TYPE);
                int status = intent.getIntExtra(
                        MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_STATUS, -1);
                int currentPro = intent.getIntExtra(
                        MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_CURRENTPRO, 0);
                String cellName = intent
                        .getStringExtra(MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_CURRENTNAME);
                

                if(NetworkTestApp.isBackRun_celldb){
                	//后台运行
                	hide();
                }else{
                	if (!isShown()) {
                        setVisibility(View.VISIBLE);
                    }
                }
                
                cellPro.setProgress(currentPro);
                
                if (type.equals(MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_TYPE_CSV)) {
                    // 下载文件
                    if (status == -1) {
                        // 下载失败
                        Toast.makeText(context, context.getString(R.string.downCell_failre), Toast.LENGTH_LONG).show();
                        hide();
                    } else if (status == 0) {
                        // 下载中
                        cellMsgText.setText(context.getString(R.string.downCell_ing) + currentPro + "%");
                    } else if (status == 1) {
                        // 下载完成
                        cellMsgText.setText(R.string.downCell_success);
                        hide();
                    }
                } else if (type
                        .equals(MyCommonConstant.PrivateActionName.SERVICE_DOWN_VILLAGE_TYPE_DB)) {
                    // 导入数据库
                    if (status == -1) {
                        // 下载失败
                        Toast.makeText(context, context.getString(R.string.insertCell_failure), Toast.LENGTH_LONG).show();
                        hide();
                    } else if (status == 0) {
                        // 下载中
                        cellMsgText.setText(context.getString(R.string.insertCell_ing) + cellName);
                    } else if (status == 1) {
                        // 下载完成
                        cellMsgText.setText(R.string.insertCell_success);
                        hide();
                        showDialog(mContext, context.getString(R.string.insertCell_success));
                    }
                }
            }
        }
    }
    
    /**
	 * 定义一个显示提示信息的对话框
	 * @param context
	 * @param msg
	 * @param title
	 */
	private void showDialog(Context context,String msg){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(msg)
		       .setTitle("消息提示")
		       .setCancelable(false)
		       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.dismiss();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
    
	public void show(){
		this.setVisibility(View.VISIBLE);
	}
	
	public void hide(){
		this.setVisibility(View.GONE);
	}

}
